package com.jeesite.modules.ch.service;

import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.NumberUtils;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.ch.dao.ChLiushuiDao;
import com.jeesite.modules.ch.entity.ChLiushui;
import com.jeesite.modules.ch.entity.param.LiushuiType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 渠道流水Service
 * @author 李寒冰
 * @version 2025-10-11
 */
@Service
public class ChLiushuiService extends CrudService<ChLiushuiDao, ChLiushui> {

	private final ChLiushuiDao chLiushuiDao;

	public ChLiushuiService(ChLiushuiDao chLiushuiDao) {
		this.chLiushuiDao = chLiushuiDao;
	}

	/**
	 * 获取单条数据
	 * @param chLiushui
	 * @return
	 */
	@Override
	public ChLiushui get(ChLiushui chLiushui) {
		return super.get(chLiushui);
	}
	
	/**
	 * 查询分页数据
	 * @param chLiushui 查询条件
	 * @param chLiushui page 分页对象
	 * @return
	 */
	@Override
	public Page<ChLiushui> findPage(ChLiushui chLiushui) {
		return super.findPage(chLiushui);
	}
	
	/**
	 * 查询列表数据
	 * @param chLiushui
	 * @return
	 */
	@Override
	public List<ChLiushui> findList(ChLiushui chLiushui) {
		return super.findList(chLiushui);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param chLiushui
	 */
	@Override
	@Transactional
	public void save(ChLiushui chLiushui) {
		super.save(chLiushui);
	}
	
	/**
	 * 更新状态
	 * @param chLiushui
	 */
	@Override
	@Transactional
	public void updateStatus(ChLiushui chLiushui) {
		super.updateStatus(chLiushui);
	}
	
	/**
	 * 删除数据
	 * @param chLiushui
	 */
	@Override
	@Transactional
	public void delete(ChLiushui chLiushui) {
		super.delete(chLiushui);
	}

	/**
	 * 增加得金额
	 * @param channelId
	 * @return
	 */
	public double addJine(String channelId){
		double sum = 0.00;
		ChLiushui chLiushui = new ChLiushui();
		chLiushui.setChannelId(channelId);
		//任务奖励
		chLiushui.setType(LiushuiType.TASK_LIUSHUI.getCode());
		double v = chLiushuiDao.jineSum(chLiushui);
		sum = NumberUtils.add(sum,v);
		return sum;
	}

	/**
	 * 减金额
	 * @param channelId
	 * @return
	 */
	public double jianJine(String channelId){
		double sum = 0.00;
		ChLiushui chLiushui = new ChLiushui();
		chLiushui.setChannelId(channelId);
		//结算
		chLiushui.setType(LiushuiType.JIESUAN.getCode());
		double v = chLiushuiDao.jineSum(chLiushui);
		sum = NumberUtils.add(sum,v);
		return sum;
	}

	/**
	 * 余额
	 */
	public double yue(String channelId){
		double v = addJine(channelId);
		double v1 = jianJine(channelId);
		return NumberUtils.sub(v,v1);
	}
	
}